package com.itheima;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dao.UserDao;
import com.itheima.pojo.User;
import com.sun.org.apache.xpath.internal.operations.Lt;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

@SpringBootTest
public class MybatisPlusWrapper {

    @Autowired
    private UserDao userDao;


    @Test
    public void warpper() {
        // QueryWrapper<User> wrapper = new QueryWrapper<>();

        // age > 12 &&  age < 18
        // 1
        //  wrapper.gt("age", 12).lt("age", 18);

        // 2
        // wrapper.lambda().gt(User::getAge, 12);
        // wrapper.lambda().lt(User::getAge, 18);

        // 3
        // wrapper.lambda().gt(User::getAge, 12).lt(User::getAge, 18);

        // 4
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.gt(User::getAge, 12).lt(User::getAge, 18);


        List<User> users = userDao.selectList(wrapper);
        System.out.println(users);
    }

    @Test
    public void test03() {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        // 查询年龄大于 18  或者 小于12 的信息
        // wrapper.gt(User::getAge, 18).or().lt(User::getAge, 12);

        // 查找姓 张的信息
        // wrapper.likeRight(User::getName, "张");

        //  分页第一页 一页五条  按照年龄排序
        // IPage<User> page = new Page<>(1, 5);
        // wrapper.orderByAsc(User::getAge);


        // 年龄在12 到18 岁之间
        // wrapper.between(User::getAge, 12, 18);


        // IPage<User> userIPage = userDao.selectPage(page, wrapper);

        List<User> users = userDao.selectList(wrapper);
        // System.out.println(userIPage);
        System.out.println(users);

    }


    // 分页查询  并且按照年龄排序
    @Test
    void test04() {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        IPage<User> page = new Page<>(1,5);

        wrapper.orderByAsc(User::getAge);


        System.out.println("userDao.selectPage(page, wrapper) = " + userDao.selectPage(page, wrapper));
        // System.out.println("userDao.selectList(wrapper) = " + userDao.selectList(wrapper));
    }


    @Test
    void test05() {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        IPage<User> page = new Page<>(2, 3);

        wrapper.orderByAsc(User::getAge);

        System.out.println("userDao.selectPage(page, wrapper) = " + userDao.selectPage(page, wrapper));


    }

    @Test
    void test06() {
        // 查询总条数，平均年龄  总年龄

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.select("count(*) as count");

        List<Map<String, Object>> maps = userDao.selectMaps(wrapper);

        for (Map<String, Object> map : maps) {
            Object count = map.get("count");
            System.out.println("总条目数  count = " + count);
        }

    }


}
